package com.sunzhe.train.service;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;

import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.engine.SQLPlaceholderST;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ibeetl.cms.dao.CmsCatalogDao;
import com.ibeetl.admin.core.dao.CoreFunctionDao;
import com.ibeetl.admin.core.dao.CoreMenuDao;
import com.ibeetl.admin.core.dao.CoreOrgDao;
import com.ibeetl.admin.core.dao.CoreRoleFunctionDao;
import com.ibeetl.admin.core.dao.CoreRoleMenuDao;
import com.ibeetl.admin.core.dao.CoreUserDao;
import com.ibeetl.admin.core.rbac.DataAccessFactory;
import com.ibeetl.admin.core.util.HttpRequestLocal;
import com.ibeetl.admin.core.util.beetl.DataAccessFunction;
import com.ibeetl.admin.core.util.beetl.NextDayFunction;
import com.ibeetl.member.dao.MemberDao;
import com.ibeetl.member.entity.Member;

/**
 * 系统平台功能访问入口，所有方法应该支持缓存或者快速访问
 * @author xiandafu
 */
@Service("trainPlatformService")
public class PlatformService {

    /*当前用户会话*/
    public static final String ACCESS_CURRENT_MEMBER = "train:member";
    
	@Autowired
	HttpRequestLocal httpRequestLocal;

    @Autowired
    CoreRoleFunctionDao roleFunctionDao;

    @Autowired
    CoreRoleMenuDao sysRoleMenuDao;
    @Autowired
    MemberDao memberDao;

    @Autowired
    CoreOrgDao sysOrgDao;

    @Autowired
    CoreRoleFunctionDao sysRoleFunctionDao;

    @Autowired
    CoreMenuDao sysMenuDao;
    
    @Autowired
    CmsCatalogDao sysCatalogDao;

    @Autowired
    CoreUserDao sysUserDao;

    @Autowired
    CoreFunctionDao sysFunctionDao;

    @Autowired
    SQLManager sqlManager;

    @Autowired
    DataAccessFunction dataAccessFunction;

    @Autowired
    PlatformService self;
    @Autowired
    DataAccessFactory dataAccessFactory;

    @PostConstruct
    @SuppressWarnings("unchecked")
    public void init() {
        SQLPlaceholderST.textFunList.add("function");
        //sql语句里带有此函数来判断数据权限
        sqlManager.getBeetl().getGroupTemplate().registerFunction("function", dataAccessFunction);
        sqlManager.getBeetl().getGroupTemplate().registerFunction("nextDay", new NextDayFunction());
    }

    public Member getCurrentMember(HttpSession session) {
    	Member member = (Member) session.getAttribute(PlatformService.ACCESS_CURRENT_MEMBER);
    	if (member != null) {
        	member = this.memberDao.unique(member.getId());
        }
    	return member;
    }
    
    public Member getCurrentMember() {
        Member member =  (Member) httpRequestLocal.getSessionValue(ACCESS_CURRENT_MEMBER);
        if (member != null) {
        	member = this.memberDao.unique(member.getId());
        }
        return member;
    }
    
    public void setLoginMember(Member member) {
    	httpRequestLocal.setSessionValue(PlatformService.ACCESS_CURRENT_MEMBER, member);
    }
    public void removeLoginMember() {
    	httpRequestLocal.setSessionValue(PlatformService.ACCESS_CURRENT_MEMBER, null);
    }
}
